package pl.skubicha.utils;

import java.util.ArrayList;
import java.util.List;

import pl.skubicha.domain.FinalGrades;
import pl.skubicha.domain.PartialGrades;
import pl.skubicha.domain.Users;
import pl.skubicha.response.GradesForUserDTO;

public class GradesForUserMapper {

	public static GradesForUserDTO map( final Users user, int maxGrades ) {
		final GradesForUserDTO dto = new GradesForUserDTO();
		dto.setId( user.getId() );
		dto.setFullName( user.getFullName() );

		List< PartialGrades > partialGrades = new ArrayList< PartialGrades >( maxGrades );
		List< FinalGrades > finalGrades = new ArrayList< FinalGrades >( maxGrades );

		for ( PartialGrades grade : user.getPartialGrades() ) {
			partialGrades.add( grade );
		}

		for ( FinalGrades grade : user.getFinalGrades() ) {
			finalGrades.add( grade );
		}

		while ( maxGrades > partialGrades.size() ) {
			partialGrades.add( new PartialGrades() );
		}

		while ( finalGrades.size() == 0 ) {
			finalGrades.add( new FinalGrades() );
		}

		dto.setPartialGrades( partialGrades );
		dto.setFinalGrades( finalGrades );
		return dto;
	}

	public static List< GradesForUserDTO > map( final Iterable< Users > users, int maxGrades ) {
		return mapPages( users, maxGrades );
	}

	private static List< GradesForUserDTO > mapPages( final Iterable< Users > users, int maxGrades ) {
		final List< GradesForUserDTO > dtos = new ArrayList< GradesForUserDTO >();

		for ( final Users user : users ) {
			dtos.add( map( user, maxGrades ) );
		}

		return dtos;
	}
}
